package com.tjsiqi.controller;

import com.tjsiqi.beans.PageQuery;
import com.tjsiqi.common.JsonData;
import com.tjsiqi.model.JblSendTruckOUTEXP;
import com.tjsiqi.param.SendCountParam;
import com.tjsiqi.service.SysSendOrderService;
import com.tjsiqi.util.DateTimeUtil;
import com.tjsiqi.util.ExportExcelUtil;
import com.tjsiqi.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * Created by lxc
 */

@Controller
@RequestMapping("/sys/SendOrder")
public class SysSendOrderController {

    @Resource
    private SysSendOrderService sysSendOrderService;
//  跳转发货单页面
    @RequestMapping("/SendOrder.page")
    public ModelAndView SendOrderPage() {
        return new ModelAndView("SendOrder");
    }

//  查询发货单数据
    @RequestMapping("/page.json")
    @ResponseBody
    public JsonData searchPage(SendCountParam sendCountParam, PageQuery page) {
        System.out.println("测试sendCountParam"+sendCountParam.toString());
        System.out.println("测试page"+page.toString());
        return JsonData.success(sysSendOrderService.searchPageList(sendCountParam, page));
    }
// 导出发货单
    @RequestMapping("/export")
    @ResponseBody
    public JsonData exportByParam(HttpServletRequest request, HttpServletResponse response, SendCountParam sendCountParam) throws IOException {
        String filename = "发货单" + DateTimeUtil.dateToStr(new Date(), "yyyy-MM-dd");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8") + ".xls");
        response.setContentType("application/x-download");
        ExportExcelUtil<JblSendTruckOUTEXP> util = new ExportExcelUtil<JblSendTruckOUTEXP>();
        List<JblSendTruckOUTEXP> SendOrderList = sysSendOrderService.exportByParam(sendCountParam);
        OutputStream ouputStream = response.getOutputStream();
        String[] columnNames = {"发货单号", "生产线", "合同编号", "任务单号", "配比编号", "订货单位", "工程名称", "工程部位", "工程地址", "强度等级", "特殊要求", "坍落度", "浇注方式", "本车方量", "生产方量", "累计方量", "累计车次", "车号", "司机", "运输距离", "发货时间", "操作员", "发货人", "质检员", "备注", "状态", "是否冬施", "外加剂", "工地联系人", "工地联系电话", "是否锁定", "泵车"};
        util.exportExcel("发货单列表", columnNames, SendOrderList, ouputStream, ExportExcelUtil.EXCEL_FILE_2003);
        return JsonData.success();
    }
//  移除发货单
    @RequestMapping("/remove")
    @ResponseBody
    public JsonData removeByParam(HttpServletRequest request, HttpServletResponse response, SendCountParam sendCountParam) throws IOException {
        if (StringUtils.isBlank(sendCountParam.getJbzCode())) {
            return JsonData.fail("搅拌站编号获取失败，请刷新重试！");
        } else{
            int isRemove = sysSendOrderService.removeByParam(sendCountParam);
            return JsonData.success(isRemove);
        }
    }


}
